﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Fin;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.Finance;

namespace ESOL.WinUI.Finance
{
    public partial class frmManageCashFlow : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private FinCashFlowEntity mSelectedCashFlow;
        #endregion

        #region Constructor
        public frmManageCashFlow()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindCombobox()
        {
            List<string> listYear = new List<string>();
            int year = 2011;
            while (year <= DateTime.Today.Year)
            {
                listYear.Add(year.ToString());
                year++;
            }
            for (int i = listYear.Count - 1; i >= 0; i--)
            {
                cmbYear.Items.Add(listYear[i]);
            }
            cmbYear.Text = DateTime.Today.ToString("yyyy");

            List<string> listMonth = new List<string>();
            int month = 1;
            while (month <= 12)
            {
                listMonth.Add(month.ToString().PadLeft(2, '0'));
                month++;
            }
            for (int i = 0; i < listMonth.Count; i++)
            {
                cmbMonth.Items.Add(listMonth[i]);
            }
            cmbMonth.Text = DateTime.Today.ToString("MM");

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colCURRENCYTOTAL, "CS", true);
        }
        private string GetCondition()
        {
            string condition = "";
            return condition;
        }
        private void BindDataGrid_CashFlow()
        {
            if (string.IsNullOrEmpty(cmbYear.Text) || string.IsNullOrEmpty(cmbMonth.Text))
            {
                MessageBox.Show("请先选择年月！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            string condition = GetCondition();
            DateTime date = Convert.ToDateTime(cmbYear.Text + "-" + cmbMonth.Text + "-01");
            DataTable dt = FinCashFlowBs.GetByCondition(condition, date);
            ResetDataTable(dt);
            gvCashFlow.DataSource = dt;
            BindDataGrid_Total();
        }
        private void BindDataGrid_Total()
        {
            DataSet ds = FinCashAmountBs.GetFinCashAmountAll();
            if (ds != null && ds.Tables.Count > 0)
            {
                gvTotal.DataSource = ds.Tables[0];
            }
        }
        private Dictionary<string, decimal> GetFromDataTable(DataTable dt, int fromRowIndex, int endRowIndex)
        {
            decimal thisMonth = 0;
            decimal thisYear = 0;
            decimal lastYearMonth = 0;
            Dictionary<string, decimal> result = new Dictionary<string, decimal>();
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    int index = CommonFunctions.ToInt32(dr["ITEM_INDEX"]);
                    if (index >= fromRowIndex && index <= endRowIndex)
                    {
                        thisMonth += CommonFunctions.ToDecimal(dr["THIS_MONTH_AMOUNT"]);
                        thisYear += CommonFunctions.ToDecimal(dr["THIS_YEAR_AMOUNT"]);
                        lastYearMonth += CommonFunctions.ToDecimal(dr["LAST_YEAR_MONTH_AMOUNT"]);
                    }
                }
            }
            result.Add("ThisMonth", thisMonth);
            result.Add("ThisYear", thisYear);
            result.Add("LastYearMonth", lastYearMonth);
            return result;
        }
        private Dictionary<string, decimal> GetFromDataTable(DataTable dt, int rowIndex)
        {
            decimal thisMonth = 0;
            decimal thisYear = 0;
            decimal lastYearMonth = 0;
            Dictionary<string, decimal> result = new Dictionary<string, decimal>();
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    int index = CommonFunctions.ToInt32(dr["ITEM_INDEX"]);
                    if (index == rowIndex)
                    {
                        thisMonth = CommonFunctions.ToDecimal(dr["THIS_MONTH_AMOUNT"]);
                        thisYear = CommonFunctions.ToDecimal(dr["THIS_YEAR_AMOUNT"]);
                        lastYearMonth = CommonFunctions.ToDecimal(dr["LAST_YEAR_MONTH_AMOUNT"]);
                    }
                }
            }
            result.Add("ThisMonth", thisMonth);
            result.Add("ThisYear", thisYear);
            result.Add("LastYearMonth", lastYearMonth);
            return result;
        }
        private void ResetDataTable(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                Dictionary<string, decimal> d = new Dictionary<string, decimal>();
                foreach (DataRow dr in dt.Rows)
                {
                    int index = CommonFunctions.ToInt32(dr["ITEM_INDEX"]);
                    switch (index)
                    {
                        case 5:
                            d = GetFromDataTable(dt, 1, 4);
                            dr["THIS_MONTH_AMOUNT"] = d["ThisMonth"];
                            dr["THIS_YEAR_AMOUNT"] = d["ThisYear"];
                            dr["LAST_YEAR_MONTH_AMOUNT"] = d["LastYearMonth"];
                            break;
                        case 6:
                            d = GetFromDataTable(dt, 7, 15);
                            dr["THIS_MONTH_AMOUNT"] = d["ThisMonth"];
                            dr["THIS_YEAR_AMOUNT"] = d["ThisYear"];
                            dr["LAST_YEAR_MONTH_AMOUNT"] = d["LastYearMonth"];
                            break;
                        case 25:
                            d = GetFromDataTable(dt, 26, 35);
                            dr["THIS_MONTH_AMOUNT"] = d["ThisMonth"];
                            dr["THIS_YEAR_AMOUNT"] = d["ThisYear"];
                            dr["LAST_YEAR_MONTH_AMOUNT"] = d["LastYearMonth"];
                            break;
                        case 36:
                            d = GetFromDataTable(dt, 37, 39);
                            dr["THIS_MONTH_AMOUNT"] = d["ThisMonth"];
                            dr["THIS_YEAR_AMOUNT"] = d["ThisYear"];
                            dr["LAST_YEAR_MONTH_AMOUNT"] = d["LastYearMonth"];
                            break;
                        case 43:
                            Dictionary<string, decimal> d6 = GetFromDataTable(dt, 6);
                            Dictionary<string, decimal> d16 = GetFromDataTable(dt, 16, 25);
                            Dictionary<string, decimal> d36 = GetFromDataTable(dt, 36);
                            Dictionary<string, decimal> d40 = GetFromDataTable(dt, 40, 42);
                            dr["THIS_MONTH_AMOUNT"] = d6["ThisMonth"] + d16["ThisMonth"] + d36["ThisMonth"] + d40["ThisMonth"];
                            dr["THIS_YEAR_AMOUNT"] = d6["ThisYear"] + d16["ThisYear"] + d36["ThisYear"] + d40["ThisYear"];
                            dr["LAST_YEAR_MONTH_AMOUNT"] = d6["LastYearMonth"] + d16["LastYearMonth"] + d36["LastYearMonth"] + d40["LastYearMonth"];
                            break;
                        case 44:
                            Dictionary<string, decimal> d5 = GetFromDataTable(dt, 5);
                            Dictionary<string, decimal> d43 = GetFromDataTable(dt, 43);
                            dr["THIS_MONTH_AMOUNT"] = d5["ThisMonth"] - d43["ThisMonth"];
                            dr["THIS_YEAR_AMOUNT"] = d5["ThisYear"] - d43["ThisYear"];
                            dr["LAST_YEAR_MONTH_AMOUNT"] = d5["LastYearMonth"] - d43["LastYearMonth"];
                            break;
                        case 54:
                            d = GetFromDataTable(dt, 46, 53);
                            dr["THIS_MONTH_AMOUNT"] = d["ThisMonth"];
                            dr["THIS_YEAR_AMOUNT"] = d["ThisYear"];
                            dr["LAST_YEAR_MONTH_AMOUNT"] = d["LastYearMonth"];
                            break;
                        case 68:
                            d = GetFromDataTable(dt, 55, 67);
                            dr["THIS_MONTH_AMOUNT"] = d["ThisMonth"];
                            dr["THIS_YEAR_AMOUNT"] = d["ThisYear"];
                            dr["LAST_YEAR_MONTH_AMOUNT"] = d["LastYearMonth"];
                            break;
                        case 69:
                            Dictionary<string, decimal> d54 = GetFromDataTable(dt, 54);
                            Dictionary<string, decimal> d68 = GetFromDataTable(dt, 68);
                            dr["THIS_MONTH_AMOUNT"] = d54["ThisMonth"] - d68["ThisMonth"];
                            dr["THIS_YEAR_AMOUNT"] = d54["ThisYear"] - d68["ThisYear"];
                            dr["LAST_YEAR_MONTH_AMOUNT"] = d54["LastYearMonth"] - d68["LastYearMonth"];
                            break;
                        case 74:
                            d5 = GetFromDataTable(dt, 5);
                            d54 = GetFromDataTable(dt, 54);
                            dr["THIS_MONTH_AMOUNT"] = d5["ThisMonth"] + d54["ThisMonth"];
                            dr["THIS_YEAR_AMOUNT"] = d5["ThisYear"] + d54["ThisYear"];
                            dr["LAST_YEAR_MONTH_AMOUNT"] = d5["LastYearMonth"] + d54["LastYearMonth"];
                            break;
                        case 75:
                            d43 = GetFromDataTable(dt, 43);
                            d68 = GetFromDataTable(dt, 68);
                            dr["THIS_MONTH_AMOUNT"] = d43["ThisMonth"] + d68["ThisMonth"];
                            dr["THIS_YEAR_AMOUNT"] = d43["ThisYear"] + d68["ThisYear"];
                            dr["LAST_YEAR_MONTH_AMOUNT"] = d43["LastYearMonth"] + d68["LastYearMonth"];
                            break;
                        default:
                            break;
                    }
                }
                dt.AcceptChanges();
            }
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindCombobox();
            BindDataGrid_CashFlow();
        }

        private void gridView2_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            //DataRow dr = gridView2.GetDataRow(e.RowHandle);
            //mSelectedCashFlow = new FinCashFlowEntity(dr);
        }

        protected override void Click_View()
        {
            if (mSelectedCashFlow == null)
            {
                MessageBox.Show("请选择一个现金流明细！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainCashFlow frm = new frmMaintainCashFlow(mSelectedCashFlow);
            frm.ThisActionType = ActionType.View;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mSelectedCashFlow == null)
            {
                MessageBox.Show("请选择一个现金流明细！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中现金流明细？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                bool successful = FinCashFlowBs.DeleteCashFlow(mSelectedCashFlow);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid_CashFlow();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedCashFlow == null)
            {
                MessageBox.Show("请选择一个现金流明细！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainCashFlow frm = new frmMaintainCashFlow(mSelectedCashFlow);
            frm.ThisActionType = ActionType.Modify;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid_CashFlow();
            }
        }

        protected override void Click_New()
        {
            frmMaintainCashFlow frm = new frmMaintainCashFlow();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid_CashFlow();
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid_CashFlow();
        }

        private void btnDetail_Click(object sender, EventArgs e)
        {
            frmMaintainCashFlowDetail frm = new frmMaintainCashFlowDetail();
            frm.ThisActionType = ActionType.View;
            frm.ShowDialog();
        }
        #endregion


    }
}
